package com.share.common.mbatis.plugins;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 生成空白的controller文件
 */
@Slf4j
public class ControllerGeneratorPlugin extends PluginAdapter {
    private static VarItem RestController = new VarItem("@RestController", "org.springframework.stereotype.RestController");
    private static VarItem SLF4J = new VarItem("@Slf4j", "lombok.extern.slf4j.Slf4j");

    // 项目目录，一般为 src/main/java
    private String targetProject;

    // controller包名，如：com.example.controller
    private String controllerPackage;

    // controller接口名后缀 如""
    private String controllerPreffix;

    // controller接口名后缀 如Controller
    private String controllerSuffix;

    @Override
    public boolean validate(List<String> warnings) {
        boolean valid = true;
        this.targetProject = Optional.ofNullable(properties.getProperty("targetProject")).orElse("src/main/java");
        this.controllerPackage = Optional.ofNullable(properties.getProperty("controllerPackage")).orElse("com.example.controller");
        this.controllerPreffix = Optional.ofNullable(properties.getProperty("controllerPreffix")).orElse("");
        this.controllerSuffix = Optional.ofNullable(properties.getProperty("controllerSuffix")).orElse("Controller");
        log.info("自定义插件配置获取成功!");

        String suppressGenerator = properties.getProperty("suppressGenerator");/*service生成配置的开关*/
        if ("true".equals(suppressGenerator)) {
            valid = true;
        }
        return valid;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        /*生成对应的文件*/
        List<GeneratedJavaFile> fileList = new ArrayList<>();

        /*model类名(无后缀)*/
        String packageModelName = introspectedTable.getBaseRecordType();
        String modelName = packageModelName.substring(packageModelName.lastIndexOf(".") + 1);

        /*包名.类名(无后缀)*/
        String packageControllerName = this.controllerPackage + "." + this.controllerPreffix + modelName + this.controllerSuffix;

        /*生成实现类*/
        log.info("正在配置生成XxxController类");
        FullyQualifiedJavaType controller = new FullyQualifiedJavaType(packageControllerName);
        TopLevelClass clazz = new TopLevelClass(controller);
        //描述类的作用域修饰符
        clazz.setVisibility(JavaVisibility.PUBLIC);

        //描述类 引入的类
        clazz.addImportedType(new FullyQualifiedJavaType(SLF4J.getPath()));
        clazz.addImportedType(new FullyQualifiedJavaType(RestController.getPath()));

        //添加的注解
        clazz.addAnnotation(SLF4J.getName());
        clazz.addAnnotation(RestController.getName());

        fileList.add(new GeneratedJavaFile(clazz, this.targetProject, context.getJavaFormatter()));
        return fileList;
    }
}
